package com.bases {
	import com.Event.CloseWindowEvent;
	import com.Event.SCWindowEvent;
	import com.Interface.IBase;
	import com.windows.TWindowsManager;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class TPanel extends TUIComponent implements IBase {
		protected var bg:WindowBg;
		protected var closeButton:WindowCloseButton;
		private var bitmap:SCBitmap;
		private var isBitmap:Boolean = true;
		protected var minW:uint = 270;
		protected var minH:uint = 156;
		protected var margin:Margin = new Margin(35, 10, 60, 25);

		public function TPanel(title:String = '窗口', _w:uint = 270, _h:uint = 156, _autoDispose:Boolean = false, isBitmap:Boolean = true){
			w = 270;
			h = 156;
//			if(_autoDispose){
//				this.addEventListener(Event.REMOVED_FROM_STAGE,onRemove);
//			}
			this.isBitmap = isBitmap;
			this.w = _w > minW ? _w : w;
			this.h = _h > minH ? _h : h;
			//this.width=w;
			//this.height=h;
			bg = new WindowBg(title, w, h);
			addChild(bg);
			closeButton = new WindowCloseButton();
			closeButton.addEventListener(MouseEvent.CLICK, onClickClose, false, 0, true);
			addChild(closeButton);
			this.bg.addEventListener(MouseEvent.MOUSE_DOWN, onBgMouseDown);
		}

		public function set title(value:String):void {
			this.bg.title = value;
		}

		protected function onClickClose(event:MouseEvent):void {
			event.stopImmediatePropagation();
			event.stopPropagation();
			this.dispatchEvent(new CloseWindowEvent());
			this.removeFromStage();
		}

		protected function removeFromStage():void {
			TWindowsManager.getInstance().removeWindow(this);
		}

		private function onRemove(event:Event):void {
			this.removeEventListener(Event.REMOVED_FROM_STAGE, onRemove);
			this.dispose();
		}

		override public function setSize(_w:Number, _h:Number):void {
			if (this.w == _w && this.h == _h){
				return;
			}
			this.w = _w > minW ? _w : w;
			this.h = _h > minH ? _h : h;
			this.bg.setSize(this.w, this.h);
			this.relayout();
			dispatchEvent(new SCWindowEvent(SCWindowEvent.RESIZE));
		}

		override protected function relayout():void {
			closeButton.x = this.width - closeButton.width - margin.right;
			closeButton.y = 5;
		}
		private var regX:Number;
		private var regY:Number;
		private var firstMove:Boolean = false;

		private function onBgMouseDown(event:MouseEvent):void {
			this.firstMove = true;
			regX = this.mouseX;
			regY = this.mouseY;
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			this.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}

		protected function onMouseUp(event:MouseEvent):void {
			if (this.isBitmap){
				this.firstMove = false;
				this.clearBitmap();
				for (var i:int = 0; i < this.numChildren; i++){
					this.getChildAt(i).visible = true;
				}
			}
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}

		private function onMouseMove(event:MouseEvent):void {
			if (this.isBitmap){
				if (this.firstMove){
					//进行拍照
					bitmap = SnapShoter.snapBitmap(this);
					bitmap.x = this.getBounds(this).x;
					bitmap.y = this.getBounds(this).y;
					//把所有的隐藏起来
					for (var i:int = 0; i < this.numChildren; i++){
						this.getChildAt(i).visible = false;
					}
					this.addChild(bitmap);
					this.firstMove = false;
				}
				bitmap.alpha = 0.5;
			}
			this.x = this.parent.mouseX - regX;
			this.y = this.parent.mouseY - regY;
		}

		private function clearBitmap():void {
			if (this.bitmap != null){
				if (this.contains(this.bitmap)){
					this.removeChild(this.bitmap);
				}
			}
			if (bitmap != null){
				bitmap.dispose();
				bitmap = null;
			}
		}

		override public function get height():Number {
			return bg.height;
		}
	}
}